package com.castle.fortress.admin.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.cms.dto.CmsBannerDto;
import com.castle.fortress.admin.cms.entity.CmsBannerEntity;
import com.castle.fortress.admin.cms.mapper.CmsBannerMapper;
import com.castle.fortress.admin.cms.service.CmsBannerService;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * cms轮播图管理表 服务实现类
 *
 * @author castle
 * @since 2022-02-24
 */
@Service
public class CmsBannerServiceImpl extends ServiceImpl<CmsBannerMapper, CmsBannerEntity> implements CmsBannerService {
    /**
    * 获取查询条件
    * @param cmsBannerDto
    * @return
    */
    public QueryWrapper<CmsBannerEntity> getWrapper(CmsBannerDto cmsBannerDto){
        QueryWrapper<CmsBannerEntity> wrapper= new QueryWrapper();
        if(cmsBannerDto != null){
            CmsBannerEntity cmsBannerEntity = ConvertUtil.transformObj(cmsBannerDto,CmsBannerEntity.class);
            wrapper.like(StrUtil.isNotEmpty(cmsBannerEntity.getTags()),"tags",cmsBannerEntity.getTags());
        }
        return wrapper;
    }


    @Override
    public IPage<CmsBannerDto> pageCmsBanner(Page<CmsBannerDto> page, CmsBannerDto cmsBannerDto) {
        QueryWrapper<CmsBannerEntity> wrapper = getWrapper(cmsBannerDto);
        Page<CmsBannerEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<CmsBannerEntity> cmsBannerPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<CmsBannerDto> pageDto = new Page(cmsBannerPage.getCurrent(), cmsBannerPage.getSize(),cmsBannerPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(cmsBannerPage.getRecords(),CmsBannerDto.class));
        return pageDto;
    }


    @Override
    public List<CmsBannerDto> listCmsBanner(CmsBannerDto cmsBannerDto){
        QueryWrapper<CmsBannerEntity> wrapper = getWrapper(cmsBannerDto);
        List<CmsBannerEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,CmsBannerDto.class);
    }

    @Override
    public List<CmsBannerDto> cmsTagListCmsBanner(Map<String, Object> map) {
        return ConvertUtil.transformObjList(baseMapper.cmsTagListCmsBanner(map), CmsBannerDto.class);
    }
}

